<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\User as UserModel;

class Loginreg extends Controller
{
    protected $wxUesr;

    public function __construct()
    {
        $this->wxUesr = new UserModel();
    }

    public function index()
    {
        return "欢迎使用TP5.0";
    }

    /**
     * 登陆
     *
     * @return void
     */
    public function login()
    {
        // var_dump(input('post.'));
        // exit;
        $phone = input('post.phone');
        $password = input('post.pwd');

        if (!$phone || !$password) {
            return json(['status'=>0, 'msg'=>'用户名/密码不能为空']);
        }
      
        $info = db('user_wx')->field('id,password,phone')->where('phone', $phone)->find();
        
        if (!$info || md5($password) != $info['password']) {
            return json(['status' => 1, 'msg' => '用户名不存在或密码不正确']);
        }

        $time = time();

        $result = \think\Db::execute('update dean_user_wx set ltime = '. "$time".' where id = '.$info['id']);
        
        if ($result===false) {
            return json(['status'=>2, 'msg'=>'登陆失败,请稍后重试']);
        }

        return json(['status' => 4, 'msg' => '登入成功','phone'=>$phone]);
    }

    /**
     * 登出
     * 1,直接调用方法跳转:修改密码后
     * 2,ajax请求:退出按钮
     */
    public function logout()
    {
        session(null);
        return $this->fetch('index/index');
    }

    /**
     * 注册
     *
     * @return void
     */
    public function register()
    {
        if (Request::instance()->isAjax()) {
            // var_dump(input('post.'));
            // exit;
            $phone = input('post.phone');
            $pwd = input('post.pwd');
            $verify = input('post.verify');
            $mobile_code = input('post.mobile_code');
            
            if (empty($phone) || empty($pwd) || empty($verify) || empty($mobile_code)) {
                return ['status'=>0, 'msg'=>'手机号/密码/验证码不能为空'];
            }
            
            //手机验证码+手机
            if ($phone !=  session('mobile') || $mobile_code != session('mobile_code')) {
                return ['status'=>1, 'msg'=>'注册失败,手机验证码不正确'];
            }

            //注册信息写入
            $user_model = new \app\index\model\User();
            $user_model->user_name = $phone;
            $user_model->phone = $phone;
            $user_model->password = md5($pwd);
            $user_model->ctime = time();

            if ($user_model->save()) {
                session('mobile', null);
                session('mobile_code', null);

                return ['status'=>4, 'msg'=>'注册成功'];
            } else {
                return ['status'=>2, 'msg'=>'注册失败,请稍后重试'];
            }
        } else {
            if (session('?username')) {
                $this->success('您已登入', 'index/index');
            } else {
                return  $this->fetch();
            }
        }
    }

    /**
     * 获取手机验证码
     *
     * @return void
     */
    public function getMobileCode()
    {
        if (Request::instance()->isAjax()) {
            $phone = input('post.phone');
            $verify = input('post.verify');

            if (empty($phone)) {
                return ['status'=>0,'msg'=>'手机号码不能为空'];
            }

            if (empty($verify) || !captcha_check($verify)) {
                return ['status'=>0,'msg'=>'图片验证码不正确'];
            }
            //一个手机号只能注册一次
            if (model('User')->getOne(['phone' => $phone])) {
                return ['status'=>1,'msg'=>'该手机号码已完成注册,请登陆'];
            }

            //生成6位验证码
            $mobile_code = random(6, 1);

            session('mobile', $phone);
            session('mobile_code', $mobile_code);

            //发送验证码
            $sms = new \mylib\Sms();
            $result = $sms->sendSms($phone, $mobile_code);
            
            return $result;
        }
    }
}
